package br.com.reparoja.persistence;

import java.util.List;

public class Utils {
	public static <T> String join(List<T> r, String d) {
		return join(r.toArray(), d, "", "");
	}

	public static <T> String join(List<T> list, String delimiter, String start, String end) {
		return join(list.toArray(), delimiter, start, end);
	}

	public static <T> String toJSON(List<T> list) {
		return join(list, ", ", "[", "]");
	}
	
	public static String join(String r[], String d) {
		if (r.length == 0)
			return "[]";

		StringBuilder sb = new StringBuilder();

		int i;
		for (i = 0; i < r.length - 1; i++)
			sb.append(r[i] + d);

		return sb.toString() + r[i];
	}

	public static String join(Object r[], String d, String start, String end) {
		if (r.length == 0)
			return "[]";

		StringBuilder sb = new StringBuilder();

		int i;
		for (i = 0; i < r.length - 1; i++)
			sb.append(r[i].toString() + d);

		return start + sb.toString() + r[i].toString() + end;
	}
}
